<template>
  <div class="info">
    <div class="tab-list flex-a">
      <div :class="tab == item.value ? 'active item' : 'item'" v-for="item in tabList" :key="item.value"
        @click="changeTab(item.value)">
        <span>{{ item.name }}</span>
        <i></i>
      </div>
    </div>
    <div class="box">
      <!-- 服务管理 -->
      <div v-if="tab == 'fwgl'">
        <div class="flex-b">
          <!-- 按服务 -->
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" class="input"
            label-width="68px">
            <el-form-item>
              <el-select v-model="queryParams.type" @change="changeServe" placeholder="请选择服务分类">
                <el-option v-for="item in serveList" :key="item.id" :label="item.name" :value="item.id + ''">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryParams.secType" placeholder="请选择服务子分类">
                <el-option v-for="item in subServeList" :key="item.id" :label="item.name" :value="item.id + ''">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="name" class="input" v-if="specOrSer">
              <el-input v-model="queryParams.name" placeholder="请输入服务名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="name" class="input" v-if="!specOrSer">
              <el-input v-model="queryParams.name" placeholder="请输入服务名称或条形码" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
              <el-button type="primary" class="btn" size="mini" @click="jumpTo"
                v-hasPermi="['pet:prodInfo:add']">新增服务</el-button>
              <!-- <el-button v-if="!specOrSer" type="warning" size="mini" class="btn" @click="handleExport"
                v-hasPermi="['pet:prodInfo:export']">导入</el-button> -->
              <!-- <el-button v-if="!specOrSer" type="warning" size="mini" class="btn" @click="handleExport"
                v-hasPermi="['pet:prodInfo:export']">导出</el-button> -->
            </el-form-item>
          </el-form>
          <!-- <el-switch v-model="specOrSer" active-color="#FFDD03" inactive-color="#FF6B7D" active-text="按服务"
            inactive-text="按规格">
          </el-switch> -->
        </div>
        <!-- 按服务 -->
        <div v-if="specOrSer">
          <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="服务编号" align="center" prop="no" />
            <el-table-column label="服务名称" align="center" prop="name" />
            <el-table-column label="图标" align="center" prop="icon" />
            <el-table-column label="服务分类" align="center" prop="type" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['pet:info:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['pet:info:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="flex-b flex-a">
            <!-- <el-dropdown class="dropdown" placement="bottom" @command="handleCommand">
              <span class="el-dropdown-link">
                批量操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-for="item in operationList" :key="item.value" :command="item.value">{{
                  item.label
                }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown> -->
            <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
              @pagination="getList" />
          </div>
        </div>
        <!-- 按规格 -->
        <div v-if="!specOrSer">
          <el-table v-loading="loading" :data="spcList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="规格编号，条形码" align="center" prop="spcNo" />
            <el-table-column label="规格名称" align="center" prop="spcName" />
            <el-table-column label="规格拼音码" align="center" prop="spcSp" />
            <el-table-column label="零售价" align="center" prop="retPrice" />
            <el-table-column label="会员价" align="center" prop="memPrice" />
            <el-table-column label="是否参与会员打折" align="center" prop="isDisMem">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.isDisMem" disabled active-color="#FFDD03" inactive-color="#FF6B7D"
                  active-text="是" inactive-text="否">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column label="是否复购提醒" align="center" prop="isPrchRem">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.isPrchRem" disabled active-color="#FFDD03" inactive-color="#FF6B7D"
                  active-text="是" inactive-text="否">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column label="是否生成记录" align="center" prop="isAddRec">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.isAddRec" disabled active-color="#FFDD03" inactive-color="#FF6B7D"
                  active-text="是" inactive-text="否">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['pet:spc:edit']">编辑</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['pet:spc:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="">
            <!-- <el-dropdown class="dropdown" placement="bottom" @command="handleCommand">
              <span class="el-dropdown-link">
                批量操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-for="item in specPperationList" :key="item.value" :command="item.value">{{
                  item.label
                }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown> -->
            <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
              @pagination="getList" />
          </div>
        </div>
      </div>
      <!-- 分类管理 -->
      <div v-if="tab == 'flgl'">
        <classManager></classManager>
      </div>
    </div>
    <!-- 添加或修改服务信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入服务编号" />
        </el-form-item>
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入图标" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, getServeList } from "@/api/pet/info";
import { listSpc, getSpc, delSpc, addSpc, updateSpc } from "@/api/pet/spc";
import classManager from "./../class/index"
export default {
  name: "Info",
  components: { classManager },
  data() {
    return {
      tab: 'fwgl',
      specOrSer: false,
      spcList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        name: null,
        icon: null,
        type: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      operationList: [
        {
          label: '修改分类',
          value: '1'
        },
        {
          label: '删除',
          value: '2'
        },
      ],//批量操作
      operation: '',
      specPperationList: [
        {
          label: '修改零售价',
          value: '1'
        },
        {
          label: '修改会员价',
          value: '2'
        },
        {
          label: '批量是否打折',
          value: '3'
        },
        {
          label: '批量是否开启复购提醒',
          value: '4'
        },
        {
          label: '批量修改自动生成服务记录',
          value: '5'
        },
        {
          label: '批量修改所属服务',
          value: '6'
        },
        {
          label: '删除',
          value: '7'
        }
      ],
      specOperation: '',
      // 服务分类
      serveList: [],
      //服务子分类
      subServeList: [],
      tabList: [
        {
          name: '服务管理',
          value: 'fwgl'
        },
        {
          name: '服务分类',
          value: 'flgl'
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getServeList()//获取服务-子服务分类
  },
  methods: {
    // tab切换
    changeTab(value) {
      this.tab = value
    },
    //获取服务-子服务分类
    getServeList() {
      getServeList().then(res => {
        if (res.code == 200) {
          this.serveList = res.data
        }
      })
    },
    //选择服务分类-筛选子分类
    changeServe(val) {
      this.subServeList = []
      this.$set(this.queryParams, 'secType', '')
      this.serveList.forEach(item => {
        if (item.id == val) {
          this.subServeList = item.list
        }
      })
    },
    /** 查询服务信息列表 */
    getList() {
      this.loading = true;
      if (this.specOrSer) {//按服务
        listInfo(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      } else {//有规格
        listSpc(this.queryParams).then(response => {
          this.spcList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    //新增服务
    jumpTo() {
      this.$router.push({ path: '/goods/addSer', query: {} })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        name: null,
        icon: null,
        type: null,
        remark: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    // 下拉菜单
    handleCommand(command) {
      this.$message('click on item ' + command);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/goods/addSer', query: { id: row.id } })
      // this.reset();
      // const id = row.id || this.ids
      // getInfo(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改服务信息";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务信息编号为"' + ids + '"的数据项？').then(function () {
        return delInfo(ids);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.el-dropdown-menu {
  left: 240px !important;
  width: 195px !important;
}
</style>